package cn.iocoder.yudao.module.member.controller.admin.userrelation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userrelation.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userrelation.UserRelationDO;
import cn.iocoder.yudao.module.member.convert.userrelation.UserRelationConvert;
import cn.iocoder.yudao.module.member.service.userrelation.UserRelationService;

@Tag(name = "管理后台 - 户关系")
@RestController
@RequestMapping("/member/user-relation")
@Validated
public class UserRelationController {

    @Resource
    private UserRelationService userRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建户关系")
    @PreAuthorize("@ss.hasPermission('member:user-relation:create')")
    public CommonResult<Long> createUserRelation(@Valid @RequestBody UserRelationCreateReqVO createReqVO) {
        return success(userRelationService.createUserRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新户关系")
    @PreAuthorize("@ss.hasPermission('member:user-relation:update')")
    public CommonResult<Boolean> updateUserRelation(@Valid @RequestBody UserRelationUpdateReqVO updateReqVO) {
        userRelationService.updateUserRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除户关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-relation:delete')")
    public CommonResult<Boolean> deleteUserRelation(@RequestParam("id") Long id) {
        userRelationService.deleteUserRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得户关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-relation:query')")
    public CommonResult<UserRelationRespVO> getUserRelation(@RequestParam("id") Long id) {
        UserRelationDO userRelation = userRelationService.getUserRelation(id);
        return success(UserRelationConvert.INSTANCE.convert(userRelation));
    }

    @GetMapping("/list")
    @Operation(summary = "获得户关系列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:user-relation:query')")
    public CommonResult<List<UserRelationRespVO>> getUserRelationList(@RequestParam("ids") Collection<Long> ids) {
        List<UserRelationDO> list = userRelationService.getUserRelationList(ids);
        return success(UserRelationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得户关系分页")
    @PreAuthorize("@ss.hasPermission('member:user-relation:query')")
    public CommonResult<PageResult<UserRelationRespVO>> getUserRelationPage(@Valid UserRelationPageReqVO pageVO) {
        PageResult<UserRelationDO> pageResult = userRelationService.getUserRelationPage(pageVO);
        return success(UserRelationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出户关系 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportUserRelationExcel(@Valid UserRelationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserRelationDO> list = userRelationService.getUserRelationList(exportReqVO);
        // 导出 Excel
        List<UserRelationExcelVO> datas = UserRelationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "户关系.xls", "数据", UserRelationExcelVO.class, datas);
    }

}
